package com.example.apiproject.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.apiproject.dao.FriendsDao;
import com.example.apiproject.dao.UserDao;
import com.example.apiproject.entity.Friends;
import com.example.apiproject.entity.Messages;
import com.example.apiproject.entity.PostEntity;
import com.example.apiproject.entity.form.returnfriend;
import com.example.apiproject.jwt.jwtutil;
import com.example.apiproject.service.FriendsService;
import com.example.apiproject.service.UserService;
import com.example.apiproject.utils.JsonUtils;
import com.example.apiproject.utils.R;
import com.example.apiproject.utils.Res;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (Friends)表控制层
 *
 * @author makejava
 * @since 2023-09-23 13:40:57
 */
@RestController
@RequestMapping("/friends")
public class FriendsController {
    /**
     * 服务对象
     */
    @Resource
    private FriendsService friendsService;
    @Resource
    private UserService userService;
    @Autowired
    private FriendsDao friendsDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private jwtutil jwtUtils;

    /**
     * 拉黑
     *
     * @param userId
     * @param friendId(被拉黑者ID)
     * @return
     */
    @GetMapping("/ban")
    public Res<Integer> ban(int userId, int friendId){
        return Res.ok(this.friendsService.ban(userId, friendId));
    }

    /**
     * 依据双方ID查询
     *
     * @param userId
     * @param friendId
     * @return
     */
    @GetMapping("/queryFriend")
    public Res<Friends> queryFriend(int userId, int friendId){
        return Res.ok(this.friendsService.queryFriend(userId, friendId));
    }
    /**
     * 取消拉黑
     *
     * @param userId
     * @param friendId(被拉黑者ID)
     * @return
     */
    @GetMapping("/cancelBan")
    public Res<Integer> cancelBan(int userId, int friendId){
        return Res.ok(this.friendsService.cancelBan(userId, friendId));
    }

    /**
     * 一方向另一方发送好友申请
     *
     * @param userId
     * @param friendId
     * @param des
     * @return
     */
    @GetMapping("/requestTobeFriend")
    public R requestTobeFriend(int userId,int friendId,String des){
        if (userId ==friendId)
            return R.error("不能添加自己");
        if(userService.queryById(friendId)==null)
            return R.error("该用户不存在或者已被禁用");
        if(friendsDao.queryIsFriends(userId,friendId)!=null) {return R.error("您已经申请过了");}
        Friends friends=new Friends();
        friends.setUserId(userId);
        friends.setFriendId(friendId);
        friends.setStatus(0);
        friends.setSource(1);
        friends.setCreateTime(new Date());
        friends.setDes(des);
        int num=friendsService.insert(friends);
        if(num==1) {return R.ok("申请成功");}
        return R.error("申请失败");
    }

    /**
     * 添加好友并返回好友请求信息
     * @return
     */
    @GetMapping("/getNewFriendRequest")
    public R getNewFriendRequest(HttpServletRequest request){
        String token = request.getHeader("token");
        if(StringUtils.isBlank(token)){
            token = request.getParameter("token");
        }
//
        int  userId = jwtUtils.getUserId(token);
        List<Friends> number = friendsDao.queryNewFriendRequest( userId);
        if(number.isEmpty()){return R.ok("empty");}
        List<returnfriend> returnfriends=new ArrayList<>();
        number.forEach(l->{
            returnfriend returnfriend = new returnfriend();
            BeanUtils.copyProperties(l,returnfriend);
            returnfriend.setAvatar(userDao.queryAvatarById(l.getUserId()));

            returnfriend.setUsername(userDao.queryNameById(l.getUserId()));

            returnfriends.add(returnfriend);
        });
        if(!returnfriends.isEmpty()) return R.ok().put("result",returnfriends);
        return  R.error();
    }

    /**
     *
     *一方是否同意另一方好友申请
     * @param userId
     * @param friendId
     * @param agree
     * @return
     */
    @GetMapping("/updatetobeFriend")
    public R updatetobeFriend(int userId, int friendId,boolean agree){
        int result=this.friendsService.updatetobeFriend(friendId,userId,agree);
        if(result ==1){return R.ok();}
        return R.error();
    }
    /**
     * 分页查询
     *
     * @param friends 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Friends>> queryByPage(Friends friends, PageRequest pageRequest) {
        return ResponseEntity.ok(this.friendsService.queryByPage(friends, pageRequest));
    }

    @GetMapping("queryAllByUserId")
    public Res<List<Friends>> queryAllByUserId(String id) {
        return Res.ok(this.friendsService.queryAllByUserId(id));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Friends> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.friendsService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param friends 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Friends> add(Friends friends) {
        return null;
    }

    /**
     * 编辑数据
     *
     * @param friends 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Friends> edit(Friends friends) {
        return ResponseEntity.ok(this.friendsService.update(friends));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.friendsService.deleteById(id));
    }

}

